package com.yuandian.middle.platform.utils;

import cn.hutool.core.collection.CollectionUtil;
import com.yuandian.api.middle_platform.vo.ImplementationIndicatorVo;

import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

/**
 * 运营/实时 指标-导出表头工具类
 */
public class SheetHeadUtil {


	/**
	 *  对'运营'指标数据格式转化成 List<List<Object>> 格式
	 */
	public static List<List<Object>> formatOperationalData(List<Object> list) {
		List<List<Object>> operationalList = new ArrayList<>();
		if (CollectionUtil.isNotEmpty(list)) {

			for (int i = 0; i < list.size(); i++) {
				LinkedHashMap<String, Object> rowElementMap = new LinkedHashMap<>();

				// 获取基本的字段值
				LinkedHashMap<String, Object> map = (LinkedHashMap<String, Object>) list.get(i);
				rowElementMap.put("cycleTime", map.get("cycleTime"));
				rowElementMap.put("abilityName", map.get("abilityName"));
				rowElementMap.put("serviceName", map.get("serviceName"));
				rowElementMap.put("userOrgName", map.get("userOrgName"));

				// 获取 请求量 字段里面的 对应各个 量纲 的值
				Map<String, Object> reqCountMap = (LinkedHashMap<String, Object>) map.get("reqCountMap");
				rowElementMap.putAll(reqCountMap);

				// 获取 请求量 字段里面的 对应各个 量纲 的值
				Map<String, Map<String, Object>> reqFailCountMap = (LinkedHashMap<String, Map<String, Object>>) map.get("reqFailCountMap");
				Map<String, Object> reqFailSelfReasonsMap = reqFailCountMap.get("reqFailSelfReasons");
				Map<String, Object> reqFailOtherReasonsMap = reqFailCountMap.get("reqFailOtherReasonsMap");
				rowElementMap.putAll(reqFailSelfReasonsMap);
				rowElementMap.putAll(reqFailOtherReasonsMap);

				operationalList.add(new ArrayList<>(rowElementMap.values()));
			}
		}

		return operationalList;
	}

	/**
	 *  对'实时'指标数据格式转化成 List<List<Object>> 格式
	 */
	public static List<List<Object>> formatImplementationData(List<ImplementationIndicatorVo> list) {
		List<List<Object>> implementationList = new ArrayList<>();
		if (CollectionUtil.isNotEmpty(list)) {
			for (ImplementationIndicatorVo vo : list) {
				List<Object> rowElement = new ArrayList<>();
				rowElement.add(vo.getCycleTime());
				rowElement.add(vo.getAbilityName());
				rowElement.add(vo.getServiceName());
				rowElement.add(vo.getUserOrgName());
				rowElement.add(vo.getAbilityType());

				rowElement.add(vo.getApiReqCount());
				rowElement.add(vo.getApiSystemSuccessCount());
				rowElement.add(vo.getApiBusinessSuccessCount());
				rowElement.add(vo.getApiAvgServerResponseTime());

				rowElement.add(vo.getSdkReqCount());
				rowElement.add(vo.getSdkSystemSuccessCount());
				rowElement.add(vo.getSdkAvgServerResponseTime());

				rowElement.add(vo.getH5ReqCount());
				rowElement.add(vo.getH5AvgServerResponseTime());

				rowElement.add(vo.getPaasSaasLoginCount());
				rowElement.add(vo.getPaasSaasReqCount());

				implementationList.add(rowElement);
			}
		}
		return implementationList;
	}


	/**
	 * 获取 '运营' 指标 Sheet 的表头
	 */
	public static List<List<String>> getOperationalSheetHead(Map<String, Object> fieldMap) {

		List<List<String>> resultList = new ArrayList<>();
		// A 列
		List<String> lis0 = new ArrayList<>();
		lis0.add("数据时间");
		lis0.add("数据时间");
		lis0.add("数据时间");
		resultList.add(lis0);

		// B 列
		List<String> lis1 = new ArrayList<>();
		lis1.add("能力名称");
		lis1.add("能力名称");
		lis1.add("能力名称");
		resultList.add(lis1);

		// C 列
		List<String> lis2 = new ArrayList<>();
		lis2.add("服务名称");
		lis2.add("服务名称");
		lis2.add("服务名称");
		resultList.add(lis2);

		// D 列
		List<String> lis3 = new ArrayList<>();
		lis3.add("使用方名称");
		lis3.add("使用方名称");
		lis3.add("使用方名称");
		resultList.add(lis3);


		// 动态封装 调用量 和 失败调用量 的列头
		if (CollectionUtil.isNotEmpty(fieldMap) &&
				fieldMap.get("filedReqCountMap") != null &&
				fieldMap.get("filedReqFailCountMap") != null) {

			Map<String, Object> filedReqFailCountMap = (Map<String, Object>) fieldMap.get("filedReqFailCountMap");
			List<Object> filedReqCountList = (List<Object>) fieldMap.get("filedReqCountMap");
			List<Object> filedReqFailSelfReasonsList = (List<Object>) filedReqFailCountMap.get("filedReqFailSelfReasons");
			List<Object> filedReqFailOtherReasonsList = (List<Object>) filedReqFailCountMap.get("filedReqFailOtherReasonsMap");


			// 调用量 列
			for (Object objMap : filedReqCountList) {
				Map<String, Object> map = (Map<String, Object>) objMap;
				List<String> list = new ArrayList<>();
				list.add("调用量");
				list.add("调用量");
				list.add((String) map.get("label"));
				resultList.add(list);
			}


			// 调用失败量-自身原因 列
			for (Object filedSelfReasonsMap : filedReqFailSelfReasonsList) {
				Map<String, Object> map = (Map<String, Object>) filedSelfReasonsMap;				List<String> list = new ArrayList<>();
				list.add("调用失败量");
				list.add("自身原因");
				list.add((String) map.get("label"));
				resultList.add(list);
			}

			// 调用失败量-非自身原因 列
			for (Object filedOtherReasonsMap : filedReqFailOtherReasonsList) {
				Map<String, Object> map = (Map<String, Object>) filedOtherReasonsMap;				List<String> list = new ArrayList<>();
				list.add("调用失败量");
				list.add("非自身原因");
				list.add((String) map.get("label"));
				resultList.add(list);
			}

		} else {
			// 如果没有量纲则额外补充
			List<String> listA = new ArrayList<>();
			listA.add("调用量");
			listA.add("调用量");
			resultList.add(listA);

			List<String> listB = new ArrayList<>();
			listB.add("调用失败量");
			listB.add("自身原因");
			resultList.add(listB);

			List<String> listC = new ArrayList<>();
			listC.add("调用失败量");
			listC.add("非自身原因");
			resultList.add(listC);
		}

		return resultList;
	}

	/**
	 * 获取'实时'指标 Sheet的表头
	 */
	public static List<List<String>> getImplementationSheetHead() {
		List<List<String>> implementationSheetHead = new ArrayList<>();

		// A 列
		List<String> lis0 = new ArrayList<>();
		lis0.add("数据时间");
		lis0.add("数据时间");
		implementationSheetHead.add(lis0);

		// B 列
		List<String> lis1 = new ArrayList<>();
		lis1.add("能力名称");
		lis1.add("能力名称");
		implementationSheetHead.add(lis1);

		// C 列
		List<String> lis2 = new ArrayList<>();
		lis2.add("服务名称");
		lis2.add("服务名称");
		implementationSheetHead.add(lis2);

		// D 列
		List<String> lis3 = new ArrayList<>();
		lis3.add("使用方/机构名称");
		lis3.add("使用方/机构名称");
		implementationSheetHead.add(lis3);

		// E 列
		List<String> lis4 = new ArrayList<>();
		lis4.add("能力类型");
		lis4.add("能力类型");
		implementationSheetHead.add(lis4);

//================================ API =======================================
		// F 列
		List<String> list5 = new ArrayList<>();
		list5.add("API");
		list5.add("调用量");
		implementationSheetHead.add(list5);

		// G 列
		List<String> list6 = new ArrayList<>();
		list6.add("API");
		list6.add("系统级成功量");
		implementationSheetHead.add(list6);

		// H 列
		List<String> list7 = new ArrayList<>();
		list7.add("API");
		list7.add("业务级成功量");
		implementationSheetHead.add(list7);

		// I 列
		List<String> list8 = new ArrayList<>();
		list8.add("API");
		list8.add("平均响应时间");
		implementationSheetHead.add(list8);

//================================ SDK =======================================
		List<String> list9 = new ArrayList<>();
		list9.add("SDK");
		list9.add("调用量");
		implementationSheetHead.add(list9);

		List<String> list10 = new ArrayList<>();
		list10.add("SDK");
		list10.add("系统级成功量");
		implementationSheetHead.add(list10);

		List<String> list11 = new ArrayList<>();
		list11.add("SDK");
		list11.add("平均响应时间");
		implementationSheetHead.add(list11);

//================================ H5 =======================================
		List<String> list12 = new ArrayList<>();
		list12.add("H5");
		list12.add("浏览量");
		implementationSheetHead.add(list12);

		List<String> list13 = new ArrayList<>();
		list13.add("H5");
		list13.add("平均响应时间");
		implementationSheetHead.add(list13);


//================================ PaaS/Saas =======================================
		List<String> list14 = new ArrayList<>();
		list14.add("PaaS/Saas");
		list14.add("用户登录次数");
		implementationSheetHead.add(list14);

		List<String> list15 = new ArrayList<>();
		list15.add("PaaS/Saas");
		list15.add("入网请求量");
		implementationSheetHead.add(list15);

		return implementationSheetHead;
	}


}
